package com.zpark.dao.impl;

import com.zpark.dao.ISouHuDao;
import com.zpark.pojo.Ranking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @PackageName: com.zpark.dao.impl
 * @ClassName: RankingDaoImpl
 * @Description:
 * @author: RZS
 * @date: 2022/9/27  16:31
 */
@Repository
public class SouHuDaoImpl implements ISouHuDao {

    // 自动注jdbc模板
    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public List<Ranking> selectAll() {
        String sql = "select * from ranking1";
        return jdbc.query(sql, new BeanPropertyRowMapper<>(Ranking.class));
    }

    @Override
    public Ranking selectById(Integer id) {
        String sql = "select * from ranking1 where id = ?";
        return jdbc.queryForObject(sql, new BeanPropertyRowMapper<>(Ranking.class), new Object[]{id});
    }

    @Override
    public Integer insert(Ranking ranking) {
        String sql = "insert into ranking1(film_index, film_name, film_actor_name, film_feasibly) values(?, ?, ?, ?)";
        return jdbc.update(sql, ranking.getFilmIndex(), ranking.getFilmName(), ranking.getFilmActorName(), ranking.getFilmFeasibly());
    }

    @Override
    public Integer updateById(Integer id, String fName) {
        String sql = "update ranking1 set film_name = ? where id = ?";
        return jdbc.update(sql, fName, id);
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from rankinf1 where id = ?";
        return jdbc.update(sql, id);
    }
}
